$(document).ready(function(){
	// 选中/取消选中
	$(".cart-list-check>span").click(function(){
		var checked = $(this).attr("checked");
		var numObj = $("#num-"+$(this).parent().parent().attr("cartId"));
		if(checked == "true") {
			$(this).removeClass("cart-list-check-icon-yes").addClass("cart-list-check-icon-no").attr("checked", "false");
			numObj.attr("checked", "false");
		} else {
			$(this).removeClass("cart-list-check-icon-no").addClass("cart-list-check-icon-yes").attr("checked", "true");
			numObj.attr("checked", "true");
		}
		calculatePrice();
	});
	
	// 增加、减少按钮的操作
	$(".cart-list-info-num-subtraction").click(function(){
		var numObj = $("#num-" + $(this).parent().parent().parent().attr("cartId"));
		var num = parseInt(numObj.val());
		if(num < 2) {
			alert("商品数量不能小于1");
		} else {
			setNumber($(this).parent().attr("cartId"), num - 1);
		}
	});
	
	$(".cart-list-info-num-addition").click(function(){
		var numObj = $("#num-"+$(this).parent().parent().parent().attr("cartId"));
		var num = parseInt(numObj.val());
		// TODO GXG 库存判断
		setNumber($(this).parent().attr("cartId"), num + 1);
	});
	// 处理输入框直接输入的情况
	$(".cart-list input").change(function(){
		var num = $(this).val();
		if(num < 1) {
			alert("商品数量不能小于1");
			$(this).val(1);
		}
		setNumber($(this).parent().attr("cartId"), 1);
	});
	// 删除按钮点击事件
	$(".cart-list-info-del").click(function(){delProd($(this).attr("cartId"));});
	// 结算按钮
	$("#settlementBtn").click(function(){
		$("#settlementForm").submit();
	});
	
	calculatePrice();
});
/**
 * 重新计算总计金额和积分
 */
function calculatePrice() {
	var totalScore = parseFloat(0.00);
	var totalPrice = parseFloat(0.00);
	var totalNumber = parseInt(0);
	$(".cart-list input").each(function(){
		if($(this).attr("name") === "prod_num" && $(this).attr("checked") === "true") {
			totalPrice += parseInt($(this).val()) * parseFloat($(this).attr("price"));
			totalScore += parseInt($(this).val()) * parseInt($(this).attr("score"));
			totalNumber += parseInt($(this).val());
		}
	});
	$("#totalScore").html(totalScore);
	$(".tools-btm-price").html("&yen;"+totalPrice.toFixed(2));
	$("#cartNum").html(totalNumber);
}

/**
 * 删除商品后计算是不是最后一个
 */
function delProd(cartId) {
	var layerLoading;
	// 调用后台删除 del
	$.ajax({type:"POST", url: "/cart/del/"+cartId+".html",
		dataType: "json", 
		beforeSend : function() {
			layerLoading = gshopLayer.loading("数据删除中…");
		}, success: function(data) {
			if(data && data == 1) {
				// 删除显示的dom和商品在购物车的ID
				$("li[cartId='"+cartId+"'],#settlement_"+cartId).remove();
				calculatePrice();
				var hasProd = false;
				// 判断还有没有商品
				$(".cart-list input").each(function(){
					if($(this).attr("name") === "prod_num") {
						hasProd = true;
						return false;
					}
				});
				if(!hasProd) {
					$("#prod-no").show();
					$(".cart-list").hide();
					// 隐藏底部工具栏
					$("#tools-btm").hide();
					$("#tools-btm-common").show();
				}
				gshopLayer.success("删除成功");
				return false;
			} else if(data && data.code && data.code == 23) {
				window.location.href = "/login.html";
			}
			gshopLayer.fail(data.msg);
		}, error : function() {
			gshopLayer.fail("删除失败");
		}, complete : function() {
			// 关闭加载层
			layer.close(layerLoading);
		}
	});
}

/**
 * 增减数量后触发
 */
function setNumber(cartId, number) {
	var layerLoading;
	$.ajax({type:"POST", url: "/cart/setNumber/"+cartId+".html",
		data: {number: number},
		dataType: "json", 
		beforeSend : function() {
			layerLoading = gshopLayer.loading();
		}, success: function(data) {
			if(data && data.code == 1) {
				$("#num-" + cartId).val(number);
				calculatePrice();
				gshopLayer.success();
			} else if(data && data.code && data.code == 23) {
				window.location.href = "/login.html";
			} else {
				gshopLayer.fail(data.msg);
			}
		}, error : function() {
			gshopLayer.fail();
		}, complete : function() {
			// 关闭加载层
			layer.close(layerLoading);
		}
	});
}
